<template>
	<div class="page healthnext">
		<view-box id="view-box">
			<x-head></x-head>
			<div class="listbox">
				<group style="margin: 10px 0;">
					<cell title="既往史" class="list-cell"></cell>
					<x-input title="疾　　病" v-model="medicationCasesVoList.medicationName" placeholder="疾病" text-align="right"></x-input>
					<datetime title="确诊时间" v-model="medicationCasesVoList.medicationTime" placeholder="确诊时间" @on-change="datetimeAction" format="YYYY-MM-DD" start-date="1900-01-01"></datetime>
					<x-input title="手　　术" v-model="surgeryCasesVoList.surgeryName" placeholder="手术名称" text-align="right"></x-input>
					<x-input title="外　　伤" v-model="traumaCasesVoList.traumaName" placeholder="外伤" text-align="right"></x-input>
				</group>
				<group style="margin-bottom: 10px;">
					<cell title="家族史" class="list-cell"></cell>
					<x-input title="亲　　属" v-model="relationShipVoList.name" placeholder="例如：父亲、母亲等" text-align="right"></x-input>
					<x-input title="疾病名称" v-model="relationShipVoList.medicationName" placeholder="疾病名称" text-align="right"></x-input>
				</group>
				<group style="margin-bottom: 10px;">
					<cell title="遗传史" class="list-cell"></cell>
					<x-input title="疾病名称" v-model="heredityCasesVoList.heredityName" placeholder="疾病名称" text-align="right"></x-input>
				</group>
				<group class='lastgroup'>
					<cell title="用药史" class="list-cell"></cell>
					<x-input title="药物名称" v-model="medicationRecordVoList.medication_name" placeholder="药物名称" text-align="right"></x-input>
					<datetime title="用药时间" v-model="medicationRecordVoList.course" placeholder="用药时间" format="YYYY-MM-DD" start-date="1900-01-01"></datetime>
					<x-input title="用药剂量(mg)" v-model="medicationRecordVoList.dose" placeholder="用药剂量" text-align="right"></x-input>
					<x-input title="频次" v-model="medicationRecordVoList.medication_date" placeholder="一日三次" text-align="right"></x-input>
				</group>
			</div>
		</view-box>
		<div class="footer" id="btnbox">
			<router-link class="index_1" to='/healthbase' replace>上一步</router-link>
			<div class="index_2" @click="keepAction">保存</div>
		</div>
	</div>
</template>

<script>
	import xHead from '@/components/common/Head.vue'
	export default {
		components: {
			xHead
		},
		data(){
			return {
				medicationCasesVoList: {
					medicationName: '',
					medicationTime: ''
				},
				surgeryCasesVoList: {
					surgeryName: ''
				},
				traumaCasesVoList: {
					traumaName: ''
				},
				relationShipVoList: {
					name: '',
					medicationName: ''
				},
				heredityCasesVoList: {
					heredityName: ''
				},
				medicationRecordVoList: {
					medication_name: '',
					dose: '',
					medication_date: '',
					course: ''
				},
				obj: {
					// 既往史
					"medicationCasesVoList": [{
						"medicationName": "",   //  疾病名称
						"medicationTime": ''	//  疾病时间
					}],
					// 既往史
					"surgeryCasesVoList": [{
						"surgeryName": ""	//  手术名
					}],
					// 既往史
					"traumaCasesVoList": [{
						"traumaName": ""	//  外伤名
					}],
					//遗传病
					"heredityCasesVoList": [{
						"heredityName": ""  
					}],
					//  用药史
					'medicationRecordVoList': [{
						'medication_name': "",
						'dose': "",
						'medication_date': "",
						'course': ""
					}],
					//家族史
					'relationShipVoList':[{
						'name': "",
						'medicationName': ""
					}],
					"customer_id": ''
				}
			}
		},
		created(){
			this.getData()
		},
		mounted(){
			//  解决 手机键盘弹出bug
			var original = document.documentElement.clientHeight;
			window.addEventListener("resize", function() {
				var resizeHeight = document.documentElement.clientHeight;
				if(resizeHeight != original) {
					document.getElementById('btnbox').style.display = 'none'
				} else {
					document.getElementById('btnbox').style.display = 'block'
				}
			});
		},
		methods: {
			//  时间戳转时间   确诊时间
			changeTime(){
				if(this.medicationCasesVoList.medicationTime !== '') {
							function add0(m) {
								return m < 10 ? '0' + m : m
							}
							function format(shijianchuo) {
								//shijianchuo是整数，否则要parseInt转换
								var time = new Date(shijianchuo);
								var y = time.getFullYear();
								var m = time.getMonth() + 1;
								var d = time.getDate();
								return y + '-' + add0(m) + '-' + add0(d);
							}
							format(this.medicationCasesVoList.medicationTime)
							this.medicationCasesVoList.medicationTime = (format(this.medicationCasesVoList.medicationTime));
						}
			},
			//  确诊时间   时间戳转日期
			changeTime1(){
				function add0(m) {
					return m < 10 ? '0' + m : m
				}
				function format(shijianchuo) {
					//shijianchuo是整数，否则要parseInt转换
					var time = new Date(shijianchuo);
					var y = time.getFullYear();
					var m = time.getMonth() + 1;
					var d = time.getDate();
					return y + '-' + add0(m) + '-' + add0(d);
				}
				format(this.medicationCasesVoList.medicationTime)
				this.medicationCasesVoList.medicationTime = (format(this.medicationCasesVoList.medicationTime))
			},
			//  得到数据
			getData(){
				let customer_id = localStorage.getItem('customer_id')
				let param = new URLSearchParams();
				param.append('customer_id', customer_id);
				this.$http({
					method: 'post',
					url: '/Cases/list.do',
					data: param
				})
				.then(({data})=>{
					console.log(data)
					//  既往史
					let medication = data.data.medicationCasesVoList.pop();
					this.medicationCasesVoList.medicationName = medication.medicationName;
					if(medication.medicationTime !== null){
						this.medicationCasesVoList.medicationTime = medication.medicationTime;
						// 时间戳转日期
						this.changeTime1()
					}
					let medication2 = data.data.surgeryCasesVoList.pop();
					this.surgeryCasesVoList.surgeryName = medication2.surgeryName; // 手术名
					
					let medication3 = data.data.traumaCasesVoList.pop();
					this.traumaCasesVoList.traumaName = medication3.traumaName;
					
					//  家族史
					let relationShip = data.data.relationShipVoList.pop();
					this.relationShipVoList.name = relationShip.name;
					this.relationShipVoList.medicationName = relationShip.medicationName;
					
					// 遗传史
					let heredity = data.data.heredityCasesVoList.pop();
					this.heredityCasesVoList.heredityName = heredity.heredityName;
					
					// 用药史
					let recordVoList = data.data.medicationRecordVoList.pop();
//					console.log(recordVoList)
					this.medicationRecordVoList.medication_name = recordVoList.medication_name;  //  药物名称
					this.medicationRecordVoList.course = recordVoList.course;     //  用药时间
					this.medicationRecordVoList.dose = recordVoList.dose;     //  用药剂量
					this.medicationRecordVoList.medication_date = recordVoList.medication_date;   //  频次
					
				})
				.catch((err)=>{
					console.log(err)
				})
			},
			// 选择时间
			datetimeAction(value) {
//				console.log(value)
//				var timestamp1 = Date.parse(new Date(value))
//				console.log(timestamp1)
			},
			// 上一步
			prvAction(){
				this.$router.push({
					path: '/healthbase'
				})
			},
			// 保存
			keepAction(){
				let date = new Date(this.medicationCasesVoList.medicationTime);
				let timer = date.getTime();
				this.medicationCasesVoList.medicationTime = timer;
//				console.log(this.medicationCasesVoList.medicationTime)
				
				//   400  错误   格式不对
				this.obj.customer_id = localStorage.getItem('customer_id')
				
				// 既往史     疾病名称   时间
				for(let i=0;i<this.obj.medicationCasesVoList.length;i++){
					this.obj.medicationCasesVoList[i].medicationName = this.medicationCasesVoList.medicationName
					this.obj.medicationCasesVoList[i].medicationTime = this.medicationCasesVoList.medicationTime
				}
				// 既往史    手术名
				for(let k=0;k<this.obj.surgeryCasesVoList.length;k++){
					this.obj.surgeryCasesVoList[k].surgeryName = this.surgeryCasesVoList.surgeryName
				}
				// 既往史   部位
				for(let q=0;q<this.obj.traumaCasesVoList.length;q++){
					this.obj.traumaCasesVoList[q].traumaName = this.traumaCasesVoList.traumaName
				}
				// 遗传病
				for(let j=0;j<this.obj.heredityCasesVoList.length;j++){
					this.obj.heredityCasesVoList[j].heredityName = this.heredityCasesVoList.heredityName
				}
				// 用药史medication_name
				for(let m=0;m<this.obj.medicationRecordVoList.length;m++){
					console.log(this.medicationRecordVoList.medication_name)
					this.obj.medicationRecordVoList[m].medication_name = this.medicationRecordVoList.medication_name;
					this.obj.medicationRecordVoList[m].dose = this.medicationRecordVoList.dose;
					this.obj.medicationRecordVoList[m].medication_date = this.medicationRecordVoList.medication_date;
					this.obj.medicationRecordVoList[m].course = this.medicationRecordVoList.course;
				}
				//   家族史
				for(let n=0;n<this.obj.relationShipVoList.length;n++){
					this.obj.relationShipVoList[n].name = this.relationShipVoList.name;
					this.obj.relationShipVoList[n].medicationName = this.relationShipVoList.medicationName;
				}
				
				this.$http.post('/Cases/add.do', this.obj)
				.then(({data})=>{
					if(data.status == 0){
						this.$vux.toast.show({
							text: '保存档案成功!',
							type: 'success'
						})
						//  时间戳转时间   确诊时间
						this.changeTime()
						//  用药时间
						this.$router.replace({
							path: '/datamonitor'
						})
					}
				})
				.catch((err)=>{
					this.$vux.toast.show({
						text: '保存档案失败!',
						type: 'cancel'
					})
					//  时间戳转时间   确诊时间
					this.changeTime()
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.footer {
		width: 100%;
		height: 50px;
		position: absolute;
		bottom: 0;
		left: 0;
	}
	
	.footer div,.footer .index_1 {
		width: 50%;
		float: left;
		text-align: center;
		line-height: 50px;
		color: #fff;
		font-size: 14px;
	}
	
	.footer .index_1 {
		background: #F7F7FA;
		display: block;
		color: #999;
	}
	
	.footer .index_2 {
		background: rgb(58, 175, 252);
	}
	.healthnext {
		position: fixed !important;
		left: 0;
		top: 0;
		right: 0;
		z-index: 999;
		width: 100%;
		height: 100%;
		overflow: auto;
		background: #fff !important;
		
		.lastgroup {
			padding-bottom: 70px;
		}
	}
</style>